import csv
from django.core.management import BaseCommand
from interview.models import Candidate

class Command(BaseCommand):
    help = '从一个CSV文件读取候选人列表,导入到数据库中'
    # 这是我们定义的命令行参数
    def add_arguments(self, parser):
        parser.add_argument('--path',type=str)
    # 我们定义处理逻辑
    def handle(self, *args, **kwargs):
        path = kwargs['path']
        # 我们打开文件进行读写
        with open(path,'rt',encoding='gbk')as f:
            reader=csv.reader(f,dialect='excel',delimiter=';')
            # 然后我们进行遍历每一行,往数据库写入
            for row in reader:
                candidate = Candidate.objects.create(
                    username = row[0],
                    city = row[1],
                    phone = row[2],
                    born_address = row[3],
                    major = row[4],
                    degree = row[5],
                    test_score_of_general_ability = row[6],
                    paper_score= row[7]
                )
                print(candidate)
                # print(row[0])
                # print(row[1])